#!/bin/bash

if test "$OS" = 'Windows_NT'
then
  junction=`ls ../bin/createlink 2>/dev/null`
fi
test x$junction = x && junction='cp -f'

srcs=` echo $* | awk '{ for (i=1; i<NF; i++) printf "%s ", $i}'`
dest=` echo $* | awk '{ printf $NF}'`
test -z "$srcs" && exit 0
test -z "$dest" && exit 0

#if multiple sources, target must be a directory
num=`echo $srcs | awk '{ printf NF }'`
if test $num -gt 1
then
  if test ! -d $dest 
  then
    echo "The target is not a directory or does not exist!"
    exit 1;
  fi
fi

for src in $srcs
do
  while [ ! -r $src ] 
  do
#Strip one ../ from the source path
    src=`echo $src | awk -F/ '{printf "%s",$2;for (i=3;i<=NF;i++) printf "/%s",$i}'`
  done
  if test -n "$src" 
  then
    destFile=$dest
    test -d $dest && destFile=$dest/`basename $src`
    test -f $destFile && /bin/rm -f $destFile
    winSrc="$(cygpath -w "$src")"
    winDst="$(cygpath -w "$destFile")"
    # echo $junction "$winSrc" "$winDst"
    $junction "$winSrc" "$winDst"
    status=$?
    test $status -ne 0 && echo "ERROR" &&exit $status
  fi
done

exit 0
